import { createRouter, createWebHistory } from 'vue-router'
import MyLayout from '@/layout/Layout.vue'
import { useUserStore } from '@/store/modules/user'
import { Message } from '@arco-design/web-vue'
const base = import.meta.env.VITE_APP_BASE_ASSETS

export const gzyh = 'gzyh-'
export const jkpg = 'jkpg-'
export const jkjc = 'jkjc-'
export const routes = [
  {
    path: base,
    component: MyLayout,
    redirect: base + gzyh + 'trend-forecast',
    meta: {
      title: '故障演化',
      icon: 'icon-idcard',
      alwaysShow: false,
      noCache: true
    },
    children: [
      {
        path: base + gzyh + 'trend-forecast',
        component: () => import('@/views/trendForecast/index.vue'),
        name: 'trendForecast',
        meta: {
          title: '趋势预测',
          icon: 'bi-graph-up',
          alwaysShow: false,
          noCache: true
        }
      },
      {
        path: base + gzyh + 'algorithm-management',
        component: () => import('@/views/algorithmManagement/index.vue'),
        name: 'algorithmManagement',
        meta: {
          title: '算法管理',
          icon: 'bi-layout-text-window-reverse',
          alwaysShow: false,
          noCache: true
        }
      },
      {
        path: base + gzyh + 'fault-model-management',
        component: () => import('@/views/faultModelManagement/index.vue'),
        name: 'faultModelManagement',
        meta: {
          title: '故障模型管理',
          icon: 'bi-file-earmark-zip',
          alwaysShow: false,
          noCache: true
        }
      },
      {
        path: base + gzyh + 'failure-effects-analysis',
        component: () => import('@/views/failureEffectsAnalysis/index.vue'),
        name: 'failureEffectsAnalysis',
        meta: {
          title: '故障影响分析',
          icon: 'bi-flask',
          alwaysShow: false,
          noCache: true
        }
      }
    ]
  },
  {
    path: base,
    component: MyLayout,
    meta: {
      title: '健康评估',
      icon: 'icon-idcard',
      alwaysShow: false,
      noCache: true
    },
    children: [
      {
        path: base + jkpg + 'health',
        component: () => import('@/views/health/index.vue'),
        name: 'health',
        meta: {
          title: '健康态势',
          icon: 'bi-bezier',
          alwaysShow: false,
          noCache: true
        }
      },
      {
        path: base + jkpg + 'portrait',
        component: () => import('@/views/healthAssessment/index.vue'),
        name: 'portrait',
        meta: {
          title: '评估体系构建',
          icon: 'bi-layout-wtf',
          alwaysShow: false,
          noCache: true
        }
      },
      {
        path: base + jkpg + 'taskInfoManagement',
        component: () => import('@/views/taskInfoManagement/index.vue'),
        name: 'taskInfoManagement',
        meta: {
          title: '任务信息管理',
          icon: 'bi-list-task',
          alwaysShow: false,
          noCache: true
        }
      }
    ]
  },
  {
    path: base,
    component: MyLayout,
    meta: {
      title: '健康监测',
      icon: 'icon-idcard',
      alwaysShow: false,
      noCache: true
    },
    children: [
      {
        path: '',
        name: 'constellation',
        meta: {
          title: '星座关联分析',
          icon: 'icon-share-alt',
          alwaysShow: false,
          noCache: true
        },
        children: [
          {
            path: base + jkjc + 'home',
            component: () => import('@/views/constellationManagement/index.vue'),
            name: 'constellationManagement',
            meta: {
              title: '关联分析管理',
              icon: 'icon-share-alt',
              alwaysShow: false,
              noCache: true
            }
          },
          {
            path: base + jkjc + 'data',
            component: () => import('@/views/constellationManagement/timingDataCalculation.vue'),
            name: 'timingDataCalculation',
            meta: {
              title: '时序数据计算',
              icon: 'icon-link',
              alwaysShow: false,
              noCache: true
            }
          },
          {
            path: base + jkjc + 'constant',
            component: () => import('@/views/constellationManagement/constant.vue'),
            name: 'constellationManagementconstant',
            meta: {
              title: '常值监测',
              icon: 'icon-idcard',
              alwaysShow: false,
              noCache: true
            }
          }
        ]
      },
      {
        path: base + jkjc + 'threshold-warn-back',
        name: 'earlyWarningManagement',
        meta: {
          title: '多级门限预警',
          icon: 'icon-record',
          alwaysShow: false,
          noCache: true
        },
        children: [
          {
            path: base + jkjc + 'threshold-warn-back/index',
            component: () => import('@/views/earlyWarningManagement/index.vue'),
            name: 'earlyWarningManagementIndex',
            meta: {
              title: '健康监测遥测参数管理',
              icon: 'icon-mosaic',
              alwaysShow: false,
              noCache: true
            }
          },
          {
            path: base + jkjc + 'threshold-warn-back/constant',
            component: () => import('@/views/earlyWarningManagement/constant.vue'),
            name: 'earlyWarningManagementconstant',
            meta: {
              title: '多级门限管理',
              icon: 'icon-dice',
              alwaysShow: false,
              noCache: true
            }
          }
        ]
      },
      {
        path: base + jkjc + 'complex-model-back',
        name: 'modelInference',
        meta: {
          title: '复杂模型推理',
          icon: 'icon-command',
          alwaysShow: false,
          noCache: true
        },
        children: [
          {
            path: base + jkjc + 'complex-model-back/template',
            component: () => import('@/views/modelInference/template.vue'),
            name: 'template',
            meta: {
              title: '代码模板管理',
              icon: 'bi-braces-asterisk',
              alwaysShow: false,
              noCache: true
            }
          },
          {
            path: base + jkjc + 'complex-model-back/index',
            component: () => import('@/views/modelInference/index.vue'),
            name: 'modelInferenceIndex',
            meta: {
              title: '复杂模型管理',
              icon: 'bi-box-seam',
              alwaysShow: false,
              noCache: true
            }
          },
          {
            path: base + jkjc + 'complex-model-back/constant',
            component: () => import('@/views/modelInference/constant.vue'),
            name: 'constant',
            meta: {
              title: '镜像管理',
              icon: 'bi-noise-reduction',
              alwaysShow: false,
              noCache: true
            }
          }
        ]
      },
      {
        path: base + jkjc + 'data-subscription-algorithm-back',
        name: 'algorithmBinding',
        meta: {
          title: '数据订阅绑定',
          icon: 'bi-subtract',
          alwaysShow: false,
          noCache: true
        },
        children: [
          {
            path: base + jkjc + 'data-subscription-algorithm-back/constant',
            component: () => import('@/views/algorithmBinding/constant.vue'),
            name: 'algorithmBindingconstant',
            meta: {
              title: '数据订阅管理',
              icon: 'bi-substack',
              alwaysShow: false,
              noCache: true
            }
          },
          {
            path: base + jkjc + 'data-subscription-algorithm-back/bind',
            component: () => import('@/views/algorithmBinding/bind.vue'),
            name: 'algorithmBindingData',
            meta: {
              title: '数据绑定管理',
              icon: 'bi-infinity',
              alwaysShow: false,
              noCache: true
            }
          }
        ]
      },
      {
        path: base + jkjc + 'strategy-suggestion-back',
        component: () => import('@/views/strategyRecommendation/index.vue'),
        name: 'strategyRecommendationIndex',
        meta: {
          title: '处置策略建议',
          icon: 'bi-chat-left-quote',
          alwaysShow: false,
          noCache: true
        }
      }
    ]
  },
  {
    path: base + '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      hidden: true
    }
  },
  {
    path: '/404',
    name: 'NotFound',
    component: () => import('@/views/error/404.vue'),
    meta: {
      hidden: true
    },
    alias: '/:pathMatch(.*)*'
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
    meta: {
      hidden: true
    }
  }
]
const homePath = import.meta.env.VITE_APP_BASE_ASSETS
export const router = createRouter({
  history: createWebHistory(),
  base,
  routes
})
router.beforeEach((to, from, next) => {
  // 1. 处理 404 路由
  if (to.matched.length === 0) {
    // 没有匹配到路由，回退上一页或跳转 404
    if (from.name) {
      next({ name: from.name })
    } else {
      next('/404')
    }
    return
  }

  const userStore = useUserStore()
  const token = userStore.token

  if (to.path === `${homePath}login`) {
    if (token) {
      next(homePath)
    } else {
      next()
    }
  } else {
    if (!token) {
      next(`${homePath}login`)
      Message.info('请先登录!')
    } else {
      next()
    }
  }
})
export default router
